<?php

namespace app\admin\business;
use app\common\model\mysql\AdminUser as AdminUserModel;
class AdminUser
{
    public $adminUserObj = null;
    public function __construct()
    {
        $this->adminUserObj = new AdminUserModel();
    }

    public function login($data)
    {
        $adminUser = $this->getAdminUserByUsername($data['username']);

        if($adminUser['password'] != md5($data['password']."_singwa_abc")){
            throw new \think\Exception("密码错误");
        }

        $updateDate = [
            "update_time" => time(),
            "last_login_time" => time(),
            "last_login_ip" => request()->ip()
        ];
        $res = $this->adminUserObj->updateById($adminUser['id'],$updateDate);
        if(empty($res)){
            throw new \think\Exception("登录失败");
        }

        session(config("admin.session_admin"),$adminUser);
        return true;
    }

    public function getAdminUserByUsername($username)
    {
        $adminUser = $this->adminUserObj->getAdminUserByUsername($username);
        if(empty($adminUser) || $adminUser->status != config("status.mysql.table_normal")){
            throw new \think\Exception("用户不存在或禁止登录");
            //return [];
        }

        $adminUser = $adminUser->toArray();
        return $adminUser;
    }
}